package com.yanqu.road.server.cmd.activity.daomu.pvp;

import com.yanqu.road.entity.activity.daomu.config.DaoMuActivityConfig;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.DaoMuPvpProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.daomu.DaoMuActivityModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.daomu.DaoMuActivityMgr;
import com.yanqu.road.server.manger.activity.daomu.DaoMuActivityUser;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = GameProtocol.S_DAOMU_PVP_USER_APPLY, desc = "pvp报名")
public class DaoMuPvpUserApplyCmd extends AbstractCommand {


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DaoMuPvpProto.DaoMuPvpUserApplyReqMsg reqMsg = DaoMuPvpProto.DaoMuPvpUserApplyReqMsg.parseFrom(packet.getBytes());
        //获取活动配置
        DaoMuActivityConfig config = DaoMuActivityMgr.getActivityConfig(reqMsg.getActivityId());
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST;
            return;
        }
        DaoMuActivityUser daoMuActivityUser = player.getModule(DaoMuActivityModule.class).getDaoMuActivityUser(config.getActivityId());
        if (daoMuActivityUser == null) {
            ret = GameErrorCode.E_DAOMU_PLAYER_NOT_EXIST;
            return;
        }
        // 加点东西
        DaoMuPvpProto.DaoMuPvpUserApplyReqMsg.Builder builder = reqMsg.toBuilder();
        builder.setCollectionLevel(daoMuActivityUser.getTouchGoldLevel());
        builder.setCollectionScore(daoMuActivityUser.getCollectionScore());
        builder.setPlayerInfo(PlayerPb.parsePlayerBaseTempMsg(player.getUserInfo()));
        // 上跨服
        player.sendPacket(Protocol.C_CROSS_DAOMU_PVP_USER_APPLY, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            DaoMuPvpProto.DaoMuPvpUserApplyRespMsg.Builder respMsg = DaoMuPvpProto.DaoMuPvpUserApplyRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_DAOMU_PVP_USER_APPLY, respMsg);
        }
    }
}
